<?php
$__appuser = APP_ROOT  . DS . 'app' . DS . 'config' . DS . 'UserDefined.php';
$__frameuser = FRAMEWORK_ROOT . DS . 'config' . DS . 'UserDefined.php';		
if (file_exists($__appuser)) {
    require $__appuser;
}
if (file_exists($__frameuser)) {
    require $__frameuser;
}
unset($__appuser,$__frameuser);

class Compile {
	public $_psptags = null;

	public $controller = null;
	
	public $View = null;

		

	public $activePaths = array();	
	
	public $IsActiveBase = true;

		
	public $IsloadHelperAndApphelper = false;

	public $_PspLayoutCount = 0;	
	
	public $_SelectCache = array();	
	
	public function __construct(&$View,&$controller) {
		$this->controller = $controller;
		$this->View = $View;		
	}
	

	public function _template($fileContent) {
		$fileContent = preg_replace('/^(\xef\xbb\xbf)/', '', $fileContent); //EFBBBF   
		$fileContent = preg_replace("/\<psp\:tags\s*(.+?)\s*\/\>/ies", "\$this->_PspToFormOrTags('\\1','tags')", $fileContent);
		$fileContent = preg_replace("/\<psp\:startLayout\>/ies", "\$this->_PspSELayoutToHtimDiv('start')", $fileContent);
		$fileContent = preg_replace("/\<\/psp\:startLayout\>/ies", "\$this->_PspSELayoutToHtimDiv('end')", $fileContent);
		
		$fileContent = preg_replace("/\<psp\:layout\s*(.+?)\s*\>/ies", "\$this->_PspToFormOrTags('\\1','layout')", $fileContent);		
		$fileContent = preg_replace("/\<\/psp\:layout\>/ies", "\$this->_PspLayoutToHtimDiv()", $fileContent);	

		$fileContent = preg_replace("/\<psp\:select\s*(.+?)\s*\>/ies", "\$this->_PspToFormOrTags('\\1','select')", $fileContent);		
		$fileContent = preg_replace("/\<\/psp\:select\s*\>/is", "<?php }} ?>", $fileContent);		
		$fileContent = preg_replace("/\<user\:\s*(.+?)\s*\/\>/ies", "\$this->_PspUserDefined('\\1')", $fileContent);		
		$fileContent = preg_replace("/\<psp\:form\s*(.+?)\s*\/\>/ies", "\$this->_PspToFormOrTags('\\1','form')", $fileContent);	
		$fileContent = preg_replace("/\<psp\:frame\s*(.+?)\s*\/\>/ies", "\$this->_PspToFrame('\\1')", $fileContent);			
		return $fileContent;
	}

	public function _PspUserDefined($str = '') {
		$str = trim($str);
	    if( empty($str) ){
		 return '';
		}	    	
	    $temp =array();	
	    $temp = explode(":",$str);			
        $temp = filterEmptyVar($temp);
		if(!isset($temp[0])){
			return '';
		}        
		$func = $temp[0];
		if(isset($temp[1])){
			$params = $temp[1];				
			if (function_exists('_User_'.$func)) {
				$func = '_User_'.$func;
				$retrun = $func( $this->controller,$this->View,$params );
	            return trim($retrun);
			}			
		}else{
			if (function_exists('_User_'.$func)) {
				$func = '_User_'.$func;
				$retrun = $func( $this->controller,$this->View );
	            return trim($retrun);
			}
		}	
	}

	public function _PspToFrame($str) {
	    if( empty($str) ){
		 return '';
		}
		if ($this->_psptags == null){ 
		    Loader::import('pspframework.utils.Tags');
			$this->_psptags = new PspTags($this->View);
		}
		$config = filterEmptyVar($this->_psptags->strAttribute($str));
        if(isset($config['controller'])) { $controller = $config['controller']; }else{ $controller = '';}
		if(isset($config['action'])) { $action = $config['action']; }else{  $action = '';}
		if(isset($config['iframeid'])) { $iframeID = $config['iframeid']; }else{ $iframeID = '';}
		if(isset($config['loadimg']) ) { $loadImg = $config['loadimg']; }else{ $loadImg = '';}
		if(isset($config['query']) ) { $query = $config['query'];  }else{ $query = '';}	
		
        if( empty($controller) || empty($action) || empty($iframeID) ) return ''; 	
		$iframeidstr = $iframeID;
        if(strpos($iframeID,'$')===0) {
		    $iframeidstr = '<?php echo '.$iframeID.';?>';
		}
        return  '<SCRIPT LANGUAGE="JavaScript">
                <!--
                document.write(\'<span id="'.$iframeidstr.'">'.(!empty($loadImg)? '<img alt="Loading..." src="<?php echo '.$loadImg.';?>">': 'Loading...' ).'</span>\');
                document.write(\'<div style="display:none;"><iframe src="<?php echo $this->_url("'.$controller.'/'.$action.'/?iframeID='.$iframeID.'&'.$query.'");?>" width=0 height=0></iframe></div>\');
                --> 
                </SCRIPT>';		   	
	}
	
	public function _PspLayoutToHtimDiv() {	
	    if(PspSession::check('TagsViewStartOrStop')) return '<!--PSP::START-LAYOUTS-MOVE-end--></div></div></div><!--/PSP::START-LAYOUTS-MOVE-end-->';
	    return '</div>';
	}
	
	public function _PspSELayoutToHtimDiv($type = null) {	
	    if(PspSession::check('TagsViewStartOrStop')) {
		    $code ='';
		    if($type=='start') {			
	$code =
	'<!--PSP::START-LAYOUTS-start--><?php '.'if (!defined("PSPLAYOUT_EDIT_MANAGE_SRUE")) '.'{ ?> <?php'.' define("PSPLAYOUT_EDIT_MANAGE_SRUE",1);'.'?>'.'
	<script type="text/javascript" src="<?php echo $this->_webroot("/idrag/jquery.min.js"); ?>"></script>
	<script type="text/javascript" src="<?php echo $this->_webroot("/idrag/jquery-ui.min.js"); ?>"></script>
	<script type="text/javascript" src="<?php echo $this->_webroot("/idrag/idrag.js"); ?>"></script>	
	<div id="overlay" style="background: #000;filter: alpha(opacity=50);opacity: 0.5;display: none;position: absolute;top: 0px;left: 0px;width: 100%;height: 100%;z-index: 100;display:none;">
		<font color=red>正在更新...</font>
	</div>
	'.'<?php } ?>'.'	
    <div id="module_list">
        <input type="hidden" id="orderlist" value="<?php echo $this->_psptags->pspLayout_toplist;?>" />
        <input type="hidden" id="layouturl" value="weburl=<?php echo urlencode(Request::$here );?>&compeld=<?php echo urlencode( $this->viewViewsmanagesPath );?>&app=<?php echo Request::$params[\'controller\'];?>&act=<?php echo Request::$params[\'action\'];?>&webroot=<?php echo urlencode( $this->viewRoot );?>&pagename=<?php echo urlencode( $this->viewFilename );?>" /> 
		<!--/PSP::START-LAYOUTS-start-->';	
			}elseif($type=='end') {
			    $code = '<!--PSP::START-LAYOUTS-end--></div><!--/PSP::START-LAYOUTS-end-->';
			}
		    return $code;
		}
	    return '';
	}	
		
	
	
	public function _PspToFormOrTags($str = '',$type = null) {
	    if( empty($str) || empty($type) ){
		 return '';
		}	
			
		if ($this->_psptags == null){ 
		    Loader::import('pspframework.utils.Tags');
			$this->_psptags = new PspTags($this->View);
		}
        if ($type == 'form') {
            return $this->_psptags->pspFormToHtml($str);
        }else if ($type == 'tags') {
            return $this->_psptags->pspTagsToHtml($str);
        }else if($type == 'layout') {
		    $this->_PspLayoutCount = $this->_PspLayoutCount+1;
            return $this->_psptags->PspLayoutToHtmlDiv($str,$this->_PspLayoutCount);
        }else if ($type == 'select') {
            return $this->_psptags->pspSelectToUrl($str);
        }		
	}	

}
